package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.MyEvaluteAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.CommentGetBean;
import com.lc.luzhengtang.conn.ConnEvalute;
import com.lc.luzhengtang.conn.ConnSubmitComment;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/10.
 */
public class CommentActivity extends BaseActivity {

    @BindView(R.id.img_news)
    ImageView imgNews;

    ListView lvEvaluate;
    @BindView(R.id.img_checkbox_unselected)
    ImageView imgCheckboxUnselected;
    @BindView(R.id.btn_sendcomment)
    Button btnSendcomment;
    private MyEvaluteAdapter myEvaluteAdapter;
    private List<CommentGetBean.DataBean> evaluateList = new ArrayList<>();

    private String goods_id;
    private String ordersn;
    public static RefreshListener refreshListener;
    //图片路径集合
    private List<String> mPath;
    private List<String> photo_list = new ArrayList<>();
    JSONObject jsonObj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        lvEvaluate = (ListView) findViewById(R.id.lv_evaluate);
        goods_id = getIntent().getStringExtra("goods_id");
        ordersn = getIntent().getStringExtra("ordersn");
        Log.d(goods_id, "33333333333333333");
        Log.d(goods_id, "33333333333333333");
        Log.d(ordersn, "4444444444444444444");
        Log.d(ordersn, "4444444444444444444");
        getCommentData();
        //刷新接口设置图片路径
        //刷新接口设置图片路径
//     refreshListener = new RefreshListener() {
//
//            @Override
//            public void setPhoto(List<String> path) {
//
//                mPath = path;
//
//                photo_list.addAll(path);
//
////                gv_add_pic.setAdapter(addPicAdapter);
//                myEvaluteAdapter.notifyDataSetChanged(evaluateList);
////                List<String> list = myEvaluteAdapter.listMap.get(position);
////                list.addAll(path);
//            }
//
//        };

        refreshListener=new RefreshListener(){


            @Override
            public void setPhoto(List<String> path, int position) {
                List<String> list = myEvaluteAdapter.listMap.get(position);

                list.addAll(path);
                Log.e("CommentActivity",path.toString());

                myEvaluteAdapter.listMap.put(position, list);

                myEvaluteAdapter.addPicAdapter.notifyDataSetChanged();

                myEvaluteAdapter.notifyDataSetChanged();
            }
        };


    }

    public void getCommentData() {
        new ConnEvalute(ordersn, new AsyCallBack<CommentGetBean>() {


            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommentGetBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);

                evaluateList = commenBean.getData();
                myEvaluteAdapter = new MyEvaluteAdapter(CommentActivity.this, evaluateList) {
                    @Override
                    public void onClickItem(int position) {
                    Toast.makeText(CommentActivity.this, "呵呵", Toast.LENGTH_SHORT).show();


                    }
                };
                lvEvaluate.setAdapter(myEvaluteAdapter);
                myEvaluteAdapter.notifyDataSetChanged(evaluateList);
            }


            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(CommentActivity.this, false);


    }

    List<String> list;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {



    }


    @OnClick({R.id.img_news, R.id.img_checkbox_unselected, R.id.btn_sendcomment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                break;

            case R.id.img_checkbox_unselected:
                break;
            case R.id.btn_sendcomment:
                getSubmitData();
                break;

        }
    }

    public void getSubmitData() {
      /*  StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                sb.append(list.get(i));
            } else {
                sb.append(list.get(i));
                sb.append(separator);
            }*/
        try {

            jsonObj = new JSONObject();


            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < evaluateList.size(); i++) {
                CommentGetBean.DataBean goods = evaluateList.get(i);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("goods_id", goods.getGoods_id() + "");
                //member_id
                jsonObject.put("evaluate", "1");
                //number
                jsonObject.put("content", "呵呵");
                jsonObject.put("number", "0");

                jsonObject.put("ordersn", "234234");

                jsonObject.put("picarr", "43555555555");
                jsonArray.put(i, jsonObject);
            }
            jsonObj.put("comment", jsonArray);

            //member_id
            jsonObj.put("member_id", BaseApplication.basePreferences.readUid());
            //address
            jsonObj.put("anonymous ", "1");

        } catch (Exception e) {
            e.getMessage();
        }

        new ConnSubmitComment(jsonObj.toString(), new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                LovelyToast.makeText(CommentActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }
        ).execute(CommentActivity.this, false);


    }

  public abstract class RefreshListener {
        public abstract void setPhoto(List<String> path,int poition);
    }
}
